package com.sunwhite.herostore.cmdhandler;

import com.sunwhite.herostore.common.UserManager;
import com.sunwhite.herostore.entity.User;
import com.sunwhite.herostore.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;

public class WhoElseIsHereCmdHandler implements CmdHandler<GameMsgProtocol.WhoElseIsHereCmd> {
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.WhoElseIsHereCmd msg) {
        GameMsgProtocol.WhoElseIsHereResult.Builder resultBuilder = GameMsgProtocol.WhoElseIsHereResult.newBuilder();

        for (User currUser : UserManager.listUser()) {
            if (null == currUser) {
                continue;
            }
           //创建每个用户的移动消息
            GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState.Builder moveStateBuilder = GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState.newBuilder();
            moveStateBuilder.setFromPosX(currUser.moveState.fromPosX);
            moveStateBuilder.setFromPosY(currUser.moveState.fromPosY);
            moveStateBuilder.setToPosX(currUser.moveState.toPosX);
            moveStateBuilder.setToPosY(currUser.moveState.toPosY);
            moveStateBuilder.setStartTime(currUser.moveState.startTime);
            GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState moveState = moveStateBuilder.build();


            GameMsgProtocol.WhoElseIsHereResult.UserInfo.Builder userInfoBuilder = GameMsgProtocol.WhoElseIsHereResult.UserInfo.newBuilder();
            userInfoBuilder.setUserId(currUser.userId);
            userInfoBuilder.setHeroAvatar(currUser.heroAvatar);
            userInfoBuilder.setMoveState(moveState);
            resultBuilder.addUserInfo(userInfoBuilder);
        }

        GameMsgProtocol.WhoElseIsHereResult newResult = resultBuilder.build();
        ctx.writeAndFlush(newResult);
    }
}
